<script>
    function del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: 'POST',
                url: "{:url('delete')}",
                data: {id: id},
                dataType: 'json',
                success: function (data) {
                    var result = data;
                    if (result.err == 0) { //删除成功
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2}, function () {
                            location.reload();
                        });
                    }
                },
                error: function (data) {
                    layer.msg(data);
                },
            });
        });
    }

    function editCreate(title, url) {
        var index = layer.open({
            type: 2,
            title: title,
            content: url,
            area: ['900px', '900px'],  //弹出层页面比例
            cancel: function () {
                window.location.reload();
            }
        });
        layer.full(index);
    }

    function member_stop(id) {
        layer.confirm('确认要停用吗？', function (index) {
            $.ajax({
                type: 'post',
                url: "{:url('disable')}",
                data: {'id': id},
                success: function (res) {
                    if (res.status == 1) {
                        layer.msg('已停用!', {
                            icon: 6,
                            time: 1000,
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg('操作出错', {icon: 5, time: 1000})
                    }
                }
            })
        });
    }

    /*用户-启用*/
    function member_start(id) {
        layer.confirm('确认要启用吗？', function (index) {
            $.ajax({
                type: 'post',
                url: "{:url('enable')}",
                data: {'id': id},
                success: function (res) {
                    if (res.status == 1) {
                        layer.msg('已启用!', {
                            icon: 6,
                            time: 1000
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg('操作出错', {icon: 5, time: 1000})
                    }
                }
            })
        });
    }

    function checkupdate() {
        layer.open({
            type: 1,
            title: false,
            area: ['630px', '360px'],
            content: '<p></p><p style="padding-left:15px;font-weight: 400;color:#999;">检查更新中，请耐心等待</p>'
        });

        $.ajax({
            url: '{:url("index/upgrade")}',
            type:'post',
            success:function (data) {
                $('.layui-layer-content').append(data);
            }
        })
    }

    function runDatetimePicker() {
        $("#search-datetime-start").datetimepicker({
            language: 'zh-cn',
            format: 'yyyy-mm-dd hh:ii',
            minView: "month",
            todayBtn: 1,
            autoclose: 1,
            endDate: new Date(),
        }).on('changeDate', function (event) {
            event.preventDefault();
            event.stopPropagation();
            var startTime = event.date;
            $('#search-datetime-end').datetimepicker('setStartDate', startTime);
        });
        $("#search-datetime-end").datetimepicker({
            language: 'zh-cn',
            format: 'yyyy-mm-dd hh:ii',
            minView: "month",
            todayBtn: 1,
            autoclose: 1,
            endDate: new Date(),
        }).on('changeDate', function (event) {
            event.preventDefault();
            event.stopPropagation();
            var endTime = event.date;
            $("#search-datetime-start").datetimepicker('setEndDate', endTime);
        });
    }

    $('#cbx').click(function () {
        var cbxs = $('.cbx');
        var flag = $('#cbx').prop("checked");
        $.each(cbxs, function (index, value) {
            $(value).prop("checked", flag);
        })
    })

    function delAll(tip, controller) {
        var cbxs = $('.cbx:checked');
        var arr = [];
        $.each(cbxs, function (index, value) {
            arr.push($(value).attr('data-val'));
        })
        if (arr.length <= 0) {
            layer.msg(tip, {icon: 2});
        } else {
            $.ajax({
                type: 'post',
                url: '{:url("' + controller + '/deleteAll")}',
                data: {ids: arr},
                success(res) {
                    layer.msg('批量删除成功', {icon: 1, time: 1000}, function () {
                        location.reload();
                    });
                }
            })
        }
    }

    function clearCache() {
        $.ajax({
            url:"{:url('index/clearCache')}",
            type:'get',
            success(res) {
                layer.msg('缓存清理成功', {icon: 1, time: 1000}, function () {
                    location.reload();
                });
            },
            error(data) {
                layer.msg('缓存清理失败', {icon: 2, time: 1000}, function () {
                    location.reload();
                });
            }
        })
    }

    function subResHandle(res) {
        if (res.err == 0) {
            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                //刷新父页面
                window.parent.location.reload();
                //关闭当前弹窗
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            });
        } else {
            layer.msg(res.msg, {icon: 2, time: 1000});
        }
    }
</script>